package com.yangjun.controller.realtimeLiveAnalysis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.yangjun.dao.impl.realtimeLiveAnalysis.realtimeSource.RealtimeListImpl;
import com.yangjun.dao.mybatis.mapper.realtimeLiveAnalysis.KITListMapper;
import com.yangjun.dao.mybatis.mapper.realtimeLiveAnalysis.OrderAmountListMapper;
import com.yangjun.dao.mybatis.mapper.realtimeLiveAnalysis.VisitCountListMapper;
import com.yangjun.entity.realtimeliveAnalysis.KITListEntity;
import com.yangjun.entity.realtimeliveAnalysis.OrderAmountListEntity;
import com.yangjun.entity.realtimeliveAnalysis.VisitCountListEntity;

@Controller
@RequestMapping("/realtimeList")
public class RealtimeListController {
	
	@Autowired
    private VisitCountListMapper mVisitCountListMapper;
	
	@Autowired
    private OrderAmountListMapper mOrderAmountListMapper;
	
	@Autowired
    private KITListMapper mKITListMapper;
	
	@Autowired
    RealtimeListImpl mModel;
	/**
	 * 商品排行
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param platform	平台
	 * @param type		访客数、销售额、订单数
	 * @return
	 */
    @RequestMapping("/visitCountListMapper")
    @ResponseBody
    public PageInfo<VisitCountListEntity> visitCountListMapper(HttpServletResponse response, 
    													@RequestParam(value = "pageNum", required = true) String pageNum,
    													@RequestParam(value = "pageSize", required = true) String pageSize, 
    													@RequestParam(value = "platformstr", required = true) String platform,
    													@RequestParam(value = "type", required = true) String type) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	
    	if (StringUtils.isBlank(platform))
    		platform = "1,13,14,15,21";

    	System.err.println("platform=-="+platform);
    	PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("platform", platform);
    	map.put("type", type);
    	PageInfo<VisitCountListEntity> page = new PageInfo<VisitCountListEntity>(mVisitCountListMapper.select(map));
    	
    	return page;
    }
    
    /**
     * 科室销售榜
     * @param response
     * @param pageNum
     * @param pageSize
     * @param platform	平台
     * @param type		销售额、订单数
     * @return
     */
    @RequestMapping("/orderAmountListMapper")
    @ResponseBody
    public PageInfo<OrderAmountListEntity> orderAmountListMapper(HttpServletResponse response, 
    													@RequestParam(value = "pageNum", required = true) String pageNum,
    													@RequestParam(value = "pageSize", required = true) String pageSize, 
    													@RequestParam(value = "platformstr", required = true) String platform,
    													@RequestParam(value = "type", required = true) String type) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	
    	if (StringUtils.isBlank(platform))
    		platform = "1,13,14,15,21";

    	PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("platform", platform);
    	map.put("type", type);
    	PageInfo<OrderAmountListEntity> page = new PageInfo<OrderAmountListEntity>(mOrderAmountListMapper.select(map));
    	
    	return page;
    }
    
    /**
     * 套餐榜单
     * @param response
     * @param pageNum
     * @param pageSize
     * @param platform	平台
     * @param type		销售额、订单数
     * @param leftright		operating ：营销套餐；host：热销套餐
     * @return
     */
    @RequestMapping("/kitListMapper")
    @ResponseBody
    public Map<String, Object>  kitListMapper(HttpServletResponse response, 
    													@RequestParam(value = "pageNum", required = true) String pageNum,
    													@RequestParam(value = "pageSize", required = true) String pageSize, 
    													@RequestParam(value = "platformstr", required = true) String platform,
    													@RequestParam(value = "type", required = true) String type,
    													@RequestParam(value = "leftright", required = true) String leftright) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	Map<String, Object> resultMap = Maps.newHashMap();
    	
    	List<KITListEntity> summaryPackage = new ArrayList<KITListEntity>();
    	if (StringUtils.isBlank(platform))
    		platform = "1,13,14,15,21";

    	
//    	System.err.println("platform=="+platform);
    	PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
    	Map<String, Object> map = new HashMap<String, Object>();
    	
    	List<String> platformList = new ArrayList<String>();
    	if (StringUtils.isNotBlank(platform)){
    		platformList = Arrays.asList(platform.split(",")); 
    	}
    	map.put("platformList", platformList);
    	map.put("type", type);
    	
    	PageInfo<KITListEntity> page = null;
    	if(leftright.trim().equals("operating")){
    		page = new PageInfo<KITListEntity>(mKITListMapper.operatingPackage(map));
    		summaryPackage = mKITListMapper.operatingSummaryPackage(map);
    	}else if(leftright.trim().equals("host")){
    		page = new PageInfo<KITListEntity>(mKITListMapper.hostPackage(map));
    		summaryPackage = mKITListMapper.hostSummaryPackage(map);
    	}
    	resultMap.put("PageInfo", page);
    	resultMap.put("summaryPackage", summaryPackage.get(0));
    	return resultMap;
    }
  
    /**
     * 科室销售榜-柱状图
     * @param response
     * @param platform		平台
     * @param type			销售额、订单数
     * @param department	科室-财务、科室-运营
     * @return
     */
    @RequestMapping("/departmentBarChart")
    @ResponseBody
    public Map<String, Object> departmentBarChart(HttpServletResponse response, 
    													@RequestParam(value = "platformstr", required = true) String platform,
    													@RequestParam(value = "type", required = true) String type,
    													@RequestParam(value = "department", required = true) String department) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	
    	if (StringUtils.isBlank(platform))
    		platform = "1,13,14,15,21";
    	
    	Map<String, Object> resultMap = Maps.newHashMap();
    	
    	resultMap.put("departmentBarChart", mModel.DepartmentBarChart(platform, "", "", department, type));
    	
    	return resultMap;
    }
    
    /**
     * 套餐榜单-弹出窗口
     * @param response
     * @param platform	平台
     * @param kitcode	套餐编码或商品编码组合
     * @return
     */
    @RequestMapping("/detailWare")
    @ResponseBody
    public Map<String, Object> detailWare(HttpServletResponse response, 
    													@RequestParam(value = "platformstr", required = true) String platform,
    													@RequestParam(value = "kitcode", required = true) String kitcode,
    													@RequestParam(value = "leftright", required = true) String leftright) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	if (StringUtils.isBlank(platform))
    		platform = "1,13,14,15,21";
    	
    	Map<String, Object> resultMap = Maps.newHashMap();
    	if(leftright.trim().equals("operating")){
    		resultMap.put("kitcode", mModel.operatingDetailWare(kitcode, platform));
    	}else if(leftright.trim().equals("host")){
    		resultMap.put("kitcode", mModel.HostDetailWare(kitcode, platform));
    	}
    	
    	return resultMap;
    }
}


















